package fzu.functiongather.PullToRefresh;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import fzu.functiongather.Adapter.PulltoRefreshAdapter;
import fzu.functiongather.Model.Item_PulltoRefresh;
import fzu.functiongather.R;
import fzu.functiongather.Utils.ToastUtils;

/**
 * Created by Administrator on 2016/12/18.
 */
public class PullToRefreshActivity extends Activity implements PullToRefreshBase.OnRefreshListener2{
    private PulltoRefreshAdapter pulltoRefreshAdapter;
    private PullToRefreshListView pullToRefreshListView;
    private List<Item_PulltoRefresh> pulltoRefreshList=new ArrayList<>();
    private Item_PulltoRefresh item_pulltoRefresh;

    private int pageIndex = 1;
    private boolean isPull =false;//判断下拉刷新，清空adapter中数据
    private Boolean start=true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pulltorefresh_main);
        initView();
        initData();
    }
    private void initView(){
        pullToRefreshListView=(PullToRefreshListView)findViewById(R.id.pullListView_express_detail);
        pulltoRefreshAdapter=new PulltoRefreshAdapter(PullToRefreshActivity.this,pulltoRefreshList);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setOnRefreshListener(this);
        pullToRefreshListView.setAdapter(pulltoRefreshAdapter);
    }
    private void initData(){
        item_pulltoRefresh=new Item_PulltoRefresh();
        item_pulltoRefresh.setName("小王");
        item_pulltoRefresh.setNum(1);
        item_pulltoRefresh.setAddress("生活三区30号楼101");
        if(isPull)
            pulltoRefreshAdapter.getList().clear();
        for(int i=(pageIndex-1)*5;i<pageIndex*5;i++)
            pulltoRefreshList.add(i, item_pulltoRefresh);
        pulltoRefreshAdapter.setList(pulltoRefreshList);
        if(start) {
            pulltoRefreshAdapter.notifyDataSetChanged();
            start=false;
        }

        isPull =false;
    }

    /*
    * 下拉
    * */
    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        String label = fzu.functiongather.mUtils.DateUtils.formatDateTime(PullToRefreshActivity.this.getApplicationContext(), System.currentTimeMillis(),
                fzu.functiongather.mUtils.DateUtils.FORMAT_SHOW_TIME |  fzu.functiongather.mUtils.DateUtils.FORMAT_SHOW_DATE |  fzu.functiongather.mUtils.DateUtils.FORMAT_ABBREV_ALL);
        refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
        resetData();
        isPull = true;
        new reLoadData().execute();

    }

    /*
    * 上拉
    * */
    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        String label = fzu.functiongather.mUtils.DateUtils.formatDateTime(PullToRefreshActivity.this.getApplicationContext(), System.currentTimeMillis(),
                fzu.functiongather.mUtils.DateUtils.FORMAT_SHOW_TIME |  fzu.functiongather.mUtils.DateUtils.FORMAT_SHOW_DATE |  fzu.functiongather.mUtils.DateUtils.FORMAT_ABBREV_ALL);
      /*  int maxPage = (int) Math.ceil((double) totalCount / 10);*/
      /*  Log.i("RentHouseActivity", String.valueOf(maxPage));*/
        int maxPage=3;
        if (pageIndex < maxPage) {
            // Update the LastUpdatedLabel
            refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
            pageIndex += 1;
            new loadMoreData().execute();

        } else {
            new noMoreData().execute();
        }
    }



    //下拉刷新
    private class reLoadData extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            /*CommProgressDialog.showDialog(getContext());*/;
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            initData();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            isPull=false;
            pulltoRefreshAdapter.notifyDataSetChanged();
            pullToRefreshListView.onRefreshComplete();
            /*CommProgressDialog.commProgressDialog.dismiss();*/
            super.onPostExecute(aVoid);
        }
    }

    //上拉加载更多
    private class loadMoreData extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
           /* CommProgressDialog.showDialog(getContext());*/
        }
        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            initData();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
//            int maxPage = (int) Math.ceil((double) totalCount / 10);
//            if (pageIndex >= maxPage)
            pulltoRefreshAdapter.notifyDataSetChanged();
            pullToRefreshListView.onRefreshComplete();
            /*CommProgressDialog.commProgressDialog.dismiss();*/
            super.onPostExecute(aVoid);
        }
    }



    private class noMoreData extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
//            int maxPage = (int) Math.ceil((double) totalCount / 10);
//            if (pageIndex >= maxPage)
            ToastUtils.show(PullToRefreshActivity.this, "没有更多数据", 1);
            pullToRefreshListView.onRefreshComplete();
            super.onPostExecute(aVoid);
        }
    }

    //重置全局变量pageIndex
    private void resetData() {
        pageIndex = 1;
    }
}
